package test

import (
	"container/heap"
	"strconv"
	"strings"
)

//4.3.1
//4.1
//a<b-->0
//a==b-->1
//a>b-->2
func compare(a, b string) byte {
	as := strings.Split(a, ".")
	bs := strings.Split(b, ".")
	i := 0
	for i < len(as) && i < len(bs) {
		ai, _ := strconv.ParseInt(as[i], 10, 32)
		bi, _ := strconv.ParseInt(bs[i], 10, 32)
		if ai > bi {
			return 2
		} else if ai < bi {
			return 0
		}
		i++
	}
	if i < len(as) {
		return 2
	}
	if i < len(bs) {
		return 0
	}
	return 1
}

type heapK struct {
	count int
	arr   []int
}

func (h heapK) Len() int {
	return len(h.arr)
}

func (h heapK) Less(i, j int) bool {
	return h.arr[i] < h.arr[j]
}

func (h heapK) Swap(i, j int) {
	h.arr[i], h.arr[j] = h.arr[j], h.arr[i]
}

func (h *heapK) Push(x interface{}) {
	if h.Len() < h.count {
		h.arr = append(h.arr, x.(int))
		return
	}
	temp:=h.Pop().(int)
	if temp<x.(int){
		h.arr = append(h.arr, x.(int))
	}else {
		h.arr = append(h.arr, temp)
	}
}

func (h *heapK) Pop() interface{} {
	temp:=h.arr
	res:=temp[len(temp)-1]
	temp=temp[:len(temp)-1]
	h.arr=temp
	return res
}

func topK(array []int,k int) int {
	hpk:=&heapK{
		count: k,
		arr:   []int{},
	}
	heap.Init(hpk)
	for i:=range array{
		heap.Push(hpk,array[i])
	}
	return hpk.arr[0]
}
